package com.jic.purchase.controller;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.purchase.constant.consist.ResponseCode;
import com.jic.purchase.model.entity.PurchaseContractProductRel;
import com.jic.purchase.model.entity.PurchaseOrder;
import com.jic.purchase.model.vo.request.PurchaseOrderRequest;
import com.jic.purchase.model.vo.response.PurchaseContractProductRelResponse;
import com.jic.purchase.model.vo.response.PurchaseOrderResponse;
import com.jic.purchase.service.PurchaseContractProductRelService;
import com.jic.purchase.service.PurchaseOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 建投数据科技(山东)有限公司
 * @date: 2020-06-02 00:29:26
 */
@RestController
@Slf4j
@RequestMapping("/purchaseOrder")
@Api(tags = " [采购表]控制类-PC")
public class PurchaseOrderController {
    @Autowired
    private PurchaseOrderService service;

    @Autowired
    private PurchaseContractProductRelService purchaseContractProductRelService;

    /**
     * 新增记录
     *
     * @param request 新增[采购表]请求参数
     * @return PurchaseOrderResponse 新增[采购表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[采购表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody PurchaseOrderRequest request) throws Throwable {
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        BeanUtils.copyProperties(request, purchaseOrder);
        long flag = service.save(purchaseOrder);
        return RestResult.success(flag);
    }

    /**
     * 新增记录
     *
     * @param request 新增[采购表]请求参数
     * @return PurchaseOrderResponse 新增[采购表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增退货单")
    @PostMapping("/addReturnOrder")
    @ResponseBody
    public RestResult<List<PurchaseContractProductRelResponse>> addReturnOrder(@RequestBody PurchaseOrderRequest request) throws Throwable {
        return RestResult.success(service.addReturnOrder(request));
    }

    /**
     * 新增记录
     *
     * @param request 新增[采购表]请求参数
     * @return PurchaseOrderResponse 新增[采购表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增补货单")
    @PostMapping("/addAddOrder")
    @ResponseBody
    public RestResult<List<PurchaseContractProductRelResponse>> addAddOrder(@RequestBody PurchaseOrderRequest request) throws Throwable {
        return RestResult.success(service.addAddOrder(request));
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[采购表]请求参数
     * @return PurchaseOrderResponse 新增或修改[采购表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[采购表]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody PurchaseOrderRequest request) throws Throwable {
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        BeanUtils.copyProperties(request, purchaseOrder);
        long flag = service.saveOrUpdate(purchaseOrder);
        return RestResult.success(flag);
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[采购表]请求参数
     * @return PurchaseOrderResponse 新增或修改[采购表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新无采购单信息")
    @PostMapping("/updateNoPurchase")
    @ResponseBody
    public RestResult<Long> updateNoPurchase( @RequestBody PurchaseOrderRequest request) throws Throwable {
        long flag = service.updateNoPurchase(request);
        return RestResult.success(flag);
    }


    /**
     * 删除记录
     *
     * @param request 删除[采购表]请求参数
     * @return PurchaseOrderResponse 删除[采购表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[采购表]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody PurchaseOrderRequest request) throws Throwable {
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        BeanUtils.copyProperties(request, purchaseOrder);
        long flag = service.deleteByPrimaryKeyLogically(purchaseOrder);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[采购表]请求参数
     * @return PurchaseOrderResponse 修改[采购表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[采购表]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody PurchaseOrderRequest request) throws Throwable {
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        BeanUtils.copyProperties(request, purchaseOrder);
        long flag = service.updateByPrimaryKeySelective(purchaseOrder);
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     *
     * @param request 查询[采购表]详情请求参数
     * @return PurchaseOrderResponse 查询[采购表]详情响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[采购表]详情")
    @ResponseBody
    public RestResult<PurchaseOrderResponse> getByPrimaryKey(@Validated @RequestBody PurchaseOrderRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            PurchaseOrder purchaseOrder = new PurchaseOrder();
            BeanUtils.copyProperties(request, purchaseOrder);
            PurchaseOrder result = service.getByPrimaryKey(purchaseOrder);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            PurchaseOrderResponse bo = new PurchaseOrderResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new PurchaseOrderResponse());
    }

    /**
     * 查询无采购收货单详情
     * @param request
     * @return
     */
    @PostMapping("/getNoPurchaseOrderDetail")
    @ApiOperation(value = "查询无采购收货单详情")
    @ResponseBody
    public RestResult<PurchaseOrderResponse> getNoPurchaseOrderDetail(@Validated @RequestBody PurchaseOrderRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            PurchaseOrder purchaseOrder = new PurchaseOrder();
            BeanUtils.copyProperties(request, purchaseOrder);
            PurchaseOrder result = service.getByPrimaryKey(purchaseOrder);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            PurchaseOrderResponse bo = new PurchaseOrderResponse();
            BeanUtils.copyProperties(result, bo);
            String code = result.getCode();
            PurchaseContractProductRel rel = new PurchaseContractProductRel();
            rel.setPurchaseCode(code);
            List<PurchaseContractProductRel> productRels = purchaseContractProductRelService.listAllRecord(rel);
            if(CollectionUtil.isNotEmpty(productRels)){
                bo.setProducts(JSON.toJSONString(productRels));
            }
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new PurchaseOrderResponse());
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageResult<PurchaseOrder>> listPage(@RequestBody PurchaseOrderRequest request) throws Throwable {
        PurchaseOrder param = new PurchaseOrder();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        return RestResult.success(service.listPageBySelective(param, request.getPageNum(), request.getPageSize()));
    }

    /**
     * PURCHASE_ORDER 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [采购表] ")
    @ResponseBody
    public RestResult<List<PurchaseOrderResponse>> listPurchaseOrderAllRecord(@RequestBody PurchaseOrderRequest request) throws Throwable {
        PurchaseOrder param = new PurchaseOrder();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<PurchaseOrder> list = service.listAllRecord(param);
        List<PurchaseOrderResponse> responseList = list.stream().map(temp -> {
            PurchaseOrderResponse bo = new PurchaseOrderResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    /**
     * 新增采购单基本信息
     * @param request
     * @return
     * @throws Throwable
     */
    @ApiOperation(value = "新增采购单基本信息")
    @PostMapping("/savePurchaseOrderInfo")
    @ResponseBody
    public RestResult<Long> savePurchaseOrderInfo(@RequestBody PurchaseOrderRequest request) throws Throwable {
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        BeanUtils.copyProperties(request, purchaseOrder);
        Long flag = service.savePurchaseOrderInfo(purchaseOrder);
        return RestResult.success(flag);
    }

    /**
     * 编辑采购单基本信息
     * @param request
     * @return
     * @throws Throwable
     */
    @ApiOperation(value = "编辑采购单基本信息")
    @PostMapping("/updatePurchaseOrderInfo")
    @ResponseBody
    public RestResult<Long> updatePurchaseOrderInfo(@RequestBody PurchaseOrderRequest request) throws Throwable {
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        BeanUtils.copyProperties(request, purchaseOrder);
        Long flag = service.updatePurchaseOrderInfo(purchaseOrder);
        return RestResult.success(flag);
    }


    /**
     * 编辑采购单基本信息
     * @param request
     * @return
     * @throws Throwable
     */
    @ApiOperation(value = "编辑采购单基本信息")
    @PostMapping("/addContractIdInPurchaseOrderInfo")
    @ResponseBody
    public RestResult<Integer> addContractIdInPurchaseOrderInfo(@RequestBody PurchaseOrderRequest request) throws Throwable {
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        BeanUtils.copyProperties(request, purchaseOrder);
        int flag = service.addContractIdInPurchaseOrderInfo(purchaseOrder);
        return RestResult.success(flag);
    }
}

